<?php
#购物车
session_start();
require_once('connect.php');
if(!isset($_SESSION['car'])){
    echo "<script>alert('您还未购物');location.href='index.php';</script>";
}
$totalPrice = 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <link rel="stylesheet" href="css/shoppingCar.css">
</head>
<body>
    <table cellspacing="0">
        <caption>购物车</caption>
        <thead>
            <tr>
                <th>商品信息</th>
                <th>价格</th>
                <th>数量</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <?php
                foreach($_SESSION['car'] as $key){
                $id = $key['id'];
                $res = mysqli_query($link,"select * from tb_shangpin where id = '$id'");
                $row = mysqli_fetch_array($res);
            ?>
            <tr>
                <td class="info">
                    <img src="<?php echo $row['photo'];?>" width="44" height="59"/>
                    <p><?php echo $row['name'];?></p>
                </td>
                <td>
                    <span>
                        ￥
                        <?php
                            printf('%.02lf',$row['vipPrice']);
                            $totalPrice += $row['vipPrice']*$key['num'];
                        ?>
                    </span>
                </td>
                <td>
                    <input type="text" value="<?php echo $key['num']?>" size="2" class="num">
                </td>
                <td>
                    <a href="del.php?id=<?php echo $id;?>">取消商品</a>
                </td>
            </tr>
            <?php
                }
            ?>
        </tbody>
    </table>
    
    <div class="bottom">
        <div>
            <a href="index.php">
                <button class="btn shop">继续购物</button>
            </a>
            <a href="clear.php">
                <button class="btn clear">清空购物车</button>
            </a>
        </div>
        <div>
            <span>
                商品金额总计:￥
                <?php
                    printf('%.02lf',$totalPrice);
                    $_SESSION['totalPrice'] = $totalPrice;
                ?>
            </span>
            <a href="order.php">
                <button class="btn pay">去结算</button>
            </a>
        </div>
    </div>
    <p><?php var_dump($_SESSION['car']);?></p>
    <p><?php echo $_SESSION['totalPrice'];?></p>
</body>

</html>